package logic

import (
	"2210a/user-profile/models/mysql"
	"context"
	"fmt"
	"gorm.io/gorm"

	"2210a/user-profile/userprofilerpc/internal/svc"
	"2210a/user-profile/userprofilerpc/userprofilerpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PerfectLogic) Perfect(in *userprofilerpc.PerfectRequest) (*userprofilerpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line
	var FindUser mysql.User
	mysql.Db.Debug().Where("id = ?", in.Id).Find(&FindUser)
	if FindUser.ID == 0 {
		return nil, fmt.Errorf("个人信息修改失败")
	}
	user := mysql.User{
		Model:     gorm.Model{ID: uint(in.Id)},
		Avatar:    in.Avatar,
		Nickname:  in.Nickname,
		Mobile:    in.Mobile,
		Gender:    in.Gender,
		Birthday:  in.Birthday,
		Introduce: in.Introduce,
	}
	mysql.Db.Debug().Model(&user).Updates(&user)
	return &userprofilerpc.PerfectResponse{Id: int64(user.ID)}, nil
}
